package cn.fl.system.service.impl;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.mid.core.framework.page.Pager;
import cmm.user.facade.system.api.ISysOrgApiService;
import cmm.user.facade.system.api.ISysUsrDeviceBindApiService;
import cmm.user.facade.system.dto.SysOrgDTO;
import cmm.user.facade.system.dto.SysUsrAppealLogDTO;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.constant.WebBaseConstant;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.framework.constant.DictionaryConstant;
import cn.fl.system.service.ISysUsrAppealLogService;
import cn.fl.system.transform.SysUsrAppealLogVOTransform;
import cn.fl.system.vo.SysUsrAppealLogVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Description: 用户申诉记录表 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class SysUsrAppealLogServiceImpl implements ISysUsrAppealLogService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(SysUsrAppealLogServiceImpl.class);

	@Autowired
	private ISysUsrDeviceBindApiService sysUsrDeviceBindApiService;

	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService;

	@Autowired
	private ISysOrgApiService sysOrgApiService;


	@Override
	public SysUsrAppealLogVO selectById(Long id) throws BaseException {
		if(StringUtils.isEmpty(id)){
			return null;
		}
		SysUsrAppealLogVO vo= SysUsrAppealLogVOTransform.toVO(this.sysUsrDeviceBindApiService.getSysUsrAppealLogById(id));
		if(null != vo){
			setDictNmInfo(vo);
		}
		return vo;
	}

	@Override
	public boolean modify(SysUsrAppealLogVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "modify", "修改对象不能为空!");
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"修改对象不能为空!");
		}
		entity.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
		entity.setApprovers(CurrentThreadContext.getCurrentUserName());
		sysUsrDeviceBindApiService.modifyUsrAppealInfo(SysUsrAppealLogVOTransform.toDTO(entity));
		return true;
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page){
			page = new Pager();
		}
		Pager pager= this.sysUsrDeviceBindApiService.searchSysUsrAppealListPage(page);

		if (null != pager) {
			List<SysUsrAppealLogDTO> dtoList = (List<SysUsrAppealLogDTO>) pager.getResultList();
			List<SysUsrAppealLogVO> voList = SysUsrAppealLogVOTransform.toVOList(dtoList);
			if (CollectionUtils.isNotEmpty(voList)) {
				this.setDictNmInfo(voList);
			}
			pager.setResultList(voList);
		}
		return pager;
	}

	/**
	 * @MethodName setDictNmInfo
	 * @Description: 设置字典信息名称
	 * @Author: wangpengtao@lionbridgecapital.cn
	 * @CreateDate: 2019/8/29 17:40
	 * @Version: V1.0.0
	 * @Param voList
	 * @Return void
	 * @UpdateRemark [序号][YYYY-MM-DD] [更改人姓名][变更描述]
	 */
	private void setDictNmInfo(List<SysUsrAppealLogVO> voList) {
		if (CollectionUtils.isNotEmpty(voList)) {
			Map<String, String> frongCdMap = sysDictionaryApiService.searchGroupToMap(WebBaseConstant.SYS_SYS_CD, WebBaseConstant.SYS_P_ORG_CD, DictionaryConstant.CODE_MGT_SYS_DS_CD);
			Map<String, String> orgMap = this.searchBusinessOrgList(WebBaseConstant.SYS_SYS_CD);
			for (SysUsrAppealLogVO vo : voList) {
				if (null != frongCdMap && StringUtils.isNotEmpty(vo.getSysCd())) {
					vo.setSysCdNm(frongCdMap.get(vo.getSysCd()));
				}
				if (null != orgMap && StringUtils.isNotEmpty(vo.getBuOrgCd())) {
					vo.setBuOrgCdNm(orgMap.get(vo.getBuOrgCd()));
				}
			}
		}
	}

	private void setDictNmInfo(SysUsrAppealLogVO vo) {
		if (null != vo) {
			Map<String, String> frongCdMap = sysDictionaryApiService.searchGroupToMap(WebBaseConstant.SYS_SYS_CD, WebBaseConstant.SYS_P_ORG_CD, DictionaryConstant.CODE_MGT_SYS_DS_CD);
			Map<String, String> orgMap = this.searchBusinessOrgList(WebBaseConstant.SYS_SYS_CD);
			if (null != frongCdMap && StringUtils.isNotEmpty(vo.getSysCd())) {
				vo.setSysCdNm(frongCdMap.get(vo.getSysCd()));
			}
			if (null != orgMap && StringUtils.isNotEmpty(vo.getBuOrgCd())) {
				vo.setBuOrgCdNm(orgMap.get(vo.getBuOrgCd()));
			}
		}
	}

	/**
	 * @MethodName searchBusinessOrgList
	 * @Description: 获取部门名称
	 * @Author: wangpengtao@lionbridgecapital.cn
	 * @CreateDate: 2019/8/29 17:40
	 * @Version: V1.0.0
	 * @Param sysCd
	 * @Return java.util.Map<java.lang.String                               ,                               java.lang.String>
	 * @UpdateRemark [序号][YYYY-MM-DD] [更改人姓名][变更描述]
	 */
	private Map<String, String> searchBusinessOrgList(String sysCd) throws BaseException {
		List<SysOrgDTO> list = this.sysOrgApiService.searchListByOrgTypCd(WebBaseConstant.SYS_SYS_CD);
//		Map<String, String> sysOrgMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCmmMgtServiceSysCd(), CurrentThreadContext.getCurrentOrgCd(), DictionaryConstant.CODE_SYS_DS_CD);
//		if (null == sysOrgMap) {
//			sysOrgMap = new HashMap<>();
//		}
		Map<String, String> map = new HashMap<>();
		for (SysOrgDTO sysOrgDTO : list) {
			map.put(sysOrgDTO.getOrgCd(),  sysOrgDTO.getOrgNm());
		}
		return map;
	}
}
